const fs = require('fs');
const path = require('path');
const request = require('request');
const cheerio = require('cheerio');
let startUrl = 'http://blog.sina.com.cn/s/articlelist_2391731143_0_1.html';
const staticDir = path.join(__dirname, 'pages');

let getArticleData = (url, callback)=>{
    request(url, (err, response, body)=>{
        if(err){
            return callback(err, null);
        }else {
            let $ = cheerio.load(body);
            let targetA;
            let ArticleData = [];
            let nextPage = '';
            $('.articleCell').each((i, e)=>{
                targetA = $(e).find('.atc_title a');
                ArticleData.push({
                    title: targetA.text(),
                    href: targetA.attr('href')
                })
            });
            nextPage = $('.SG_pgnext a').attr('href');
            if(nextPage) {
                console.log(nextPage);
                getArticleData(nextPage, (err, list)=>{
                    if(err) {
                      return  callback(err, null)
                    }else {
                        callback(null,ArticleData.concat(list));
                    }
                })
            }else {
                callback(null,ArticleData);
            }
        }
    })
};
getArticleData(startUrl,function (err, data) {
    for(let item of data) {
        fs.exists(staticDir, (err, bool) => {
            if(err) {
                throw new Error(err);
            }else{
                fs.mkdir(staticDir,(err)=>{
                    if(err) {
                        throw new Error(err);
                    }else {
                        fs.writeFile(path.join(staticDir,item.title+'.html'),'test','utf8',(err)=>{
                            if(err){
                                throw new Error(err);
                            }else{
                                console.log(`${item.title}文件创建成功`);
                            }
                        })
                    }
                })
            }
        })
    }
});